package com.gitee.feizns.explore.data_structure.linked_list.singly.reverse;

/**
 * @author feizns
 * @since 2020/3/2
 */
public class Reverse2 {

    public static void main(String[] args) {
        ListNode n = n();
        ListNode l = reverseList(n);
        p(l);
    }

    private static ListNode n() {
        ListNode r = new ListNode(1);
        r.next = new ListNode(2);
        r.next.next = new ListNode(3);
        return r;
    }

    private static void p(ListNode N) {
        ListNode t = N;
        while ( t != null ) {
            System.out.print(t.val);
            t = t.next;
        }
        System.out.println();
    }

    private static ListNode LAST;

    public static ListNode reverseList(ListNode head) {
        helper(head);
        return LAST;
    }

    private static ListNode helper(ListNode head) {
        if ( head != null ) {
            if ( head.next != null ) {
                ListNode next = head.next;
                head.next = null;
                ListNode node = helper(next);
                node.next = head;
            } else {
                LAST = head;
            }
            return head;
        }
        return null;
    }

}
